﻿using System;

namespace ZyGame.Game
{
    public class FSMComponent : EntityComponent
    {
        public FSMEntry entry { get; private set; }

        public override void Awake(params object[] dataList)
        {
            entry = new FSMEntry(entity);
            if (dataList is null || dataList.Length == 0)
            {
                return;
            }

            for (int i = 0; i < dataList.Length; i++)
            {
                if (dataList[i] is FSMState state)
                {
                    Register(state);
                }
                if (dataList[i] is Type type)
                {
                    Register(type);
                }
            }
        }

        public bool HasState(string name)
        {
            return entry.HasState(name);
        }

        public bool HasState(Type type)
        {
            return entry.HasState(type);
        }

        public bool HasState<T>() where T : FSMState
        {
            return entry.HasState<T>();
        }

        public bool HasState(FSMState state)
        {
            return HasState(state.GetType());
        }

        public bool Register(FSMState state)
        {
            return entry.Register(state);
        }

        public FSMState Register(Type type)
        {
            return entry.Register(type);
        }

        public T Register<T>() where T : FSMState, new()
        {
            return entry.Register<T>();
        }

        public void Unregister(Type type)
        {
            entry.Unregister(type);
        }

        public void Unregister(string name)
        {
            entry.Unregister(name);
        }

        public void Unregister<T>() where T : FSMState
        {
            entry.Unregister<T>();
        }

        public void Unregister(FSMState state)
        {
            entry.Unregister(state);
        }

        public FSMState GetState(string name)
        {
            return entry.GetState(name);
        }

        public FSMState GetState(Type type)
        {
            return entry.GetState(type);
        }

        public T GetState<T>() where T : FSMState
        {
            return entry.GetState<T>();
        }

        public void Switch(string name)
        {
            entry.Switch(name);
        }

        public bool Switch<T>() where T : FSMState
        {
           return entry.Switch(typeof(T));
        }

        public void Switch(Type type)
        {
            entry.Switch(type);
        }

        public override void Dispose()
        {
            entry.Dispose();
        }
    }
}
